﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using QFlow.Data;

namespace QFlow.Windows.Forms
{
    public partial class UsersList : Utils.BaseForm
    {
        public UsersList(QFlowDbContext contex)
            : base(contex)
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewUser frm = new NewUser(true, this.Context);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                int userId = (gvResults.SelectedRows[0].DataBoundItem as User).UserID;
                NewUser frm = new NewUser(userId, this.Context);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                if (MessageBox.Show(QFlow.Windows.Properties.Resources.MBDeleteUser, QFlow.Windows.Properties.Resources.MBConfirmCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    try
                    {
                        User user = gvResults.SelectedRows[0].DataBoundItem as User;
                        this.Context.Users.DeleteOnSubmit(user);
                        this.Context.SubmitChanges();
                        RefreshGrid();
                    }
                    catch
                    {
                        MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantDeleteUser, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }

        private void RefreshGrid()
        {
            IEnumerable<User> lst = this.Context.Users.ToArray();

            if (!string.IsNullOrEmpty(txtName.TextBox.Text))
                lst = lst.Where(user => user.Username.Contains(txtName.TextBox.Text));
            if (!string.IsNullOrEmpty(txtDisplayName.TextBox.Text))
                lst = lst.Where(user => user.DisplayName.Contains(txtDisplayName.TextBox.Text));

            gvResults.AutoGenerateColumns = false;
            gvResults.DataSource = lst.ToArray();
        }

        private void gvResults_SelectionChanged(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }
            else
            {
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
            }

        }

        private void UsersList_Activated(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void gvResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                int userId = (gvResults.SelectedRows[0].DataBoundItem as User).UserID;
                NewUser frm = new NewUser(userId, this.Context);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }
    }
}
